module diansha {
	/**
	 * shuxing manage
	 */
	export class shuxing_manage {
		private static D: shuxing_manage = null;//only
		private shuzifactory: diansha.shuzi_factory = diansha.shuzi_factory.getinstance();
		////////////////////shuxing/////////////////////
		public liliang: diansha.shuzi = this.shuzifactory.get(0, "n");
		public minjie: diansha.shuzi = this.shuzifactory.get(0, "n");
		public zhili: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilimax: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilinow: diansha.shuzi = this.shuzifactory.get(0, "n")
		private tilihuifu: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilixiaohao: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilixiaohaojianshao: diansha.shuzi = this.shuzifactory.get(0, "n");
		private speed: diansha.shuzi = this.shuzifactory.get(0, "n");

		private jianglibaojigailv: diansha.shuzi = this.shuzifactory.get(0, "n");
		private jinjie: diansha.shuzi = this.shuzifactory.get(0, "n");
		////////////////param//////////////////////
		private tili_init: diansha.shuzi = this.shuzifactory.get(0, "n");
		private minjie_init: diansha.shuzi = this.shuzifactory.get(0, "n");
		private zhili_init: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilimax_i: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilihuifu_i: diansha.shuzi = this.shuzifactory.get(0, "n");
		private speed_i: diansha.shuzi = this.shuzifactory.get(0, "n");
		private tilixiaohaojianshao_i: diansha.shuzi = this.shuzifactory.get(0, "n");
		private jiangli_i: diansha.shuzi = this.shuzifactory.get(0, "n");
		private jiangli: diansha.shuzi = this.shuzifactory.get(1, "n");
		public constructor() {
			this.shuzifactory = diansha.shuzi_factory.getinstance();
			/**
			 * param init
			 */
			this.tili_init = this.shuzifactory.get(20, "n");
			this.minjie_init = this.shuzifactory.get(20, "n");
			this.zhili_init = this.shuzifactory.get(20, "n");
			this.tilimax_i = this.shuzifactory.get(10, "n");
			this.tilihuifu_i = this.shuzifactory.get(0.5, "n");
			this.speed_i = this.shuzifactory.get(0.03, "n");
			this.tilixiaohaojianshao_i = this.shuzifactory.get(-0.00075, "n");
			this.jiangli_i = this.shuzifactory.get(0.001, "n");
			this.jianglibaojigailv = this.shuzifactory.get(0.05, "n");
			this.jinjie = this.shuzifactory.get(0, "n");
			this.init()

		}
		/**
		 * shuxing_init
		 */
		private init() {
			this.add_liliang(this.tili_init);
			this.add_minjie(this.minjie_init);
			this.add_zhili(this.zhili_init);
			this.tilinow = this.tilimax;
		}
		/**
		 * addliliang
		 */
		private add_liliang(shuzi: diansha.shuzi) {
			this.liliang.add(shuzi);
			this.tilimax = this.liliang.cheng(this.tilimax_i);
			this.tilihuifu = this.liliang.cheng(this.tilihuifu_i);

		}
		/**
		* addminjie
		*/
		private add_minjie(shuzi: diansha.shuzi) {
			this.minjie.add(shuzi);
			this.speed = this.minjie.cheng(this.speed_i);
			this.tilixiaohaojianshao = this.minjie.cheng(this.tilixiaohaojianshao_i);
			this.tilixiaohao = this.tilixiaohao.add(this.tilixiaohaojianshao);

		}
		/**
		 * addzhili
		 */
		private add_zhili(shuzi: diansha.shuzi) {
			this.zhili.add(shuzi);
			this.jiangli = this.jiangli.cheng(this.jiangli_i);


		}
		/**
		 * wudao
		 * n:jinjie
		 */
		public wudao(wudaoshi: number, n: number): diansha.shuxingadd {
			let sb: diansha.shuxingadd = new diansha.shuxingadd();
			if (this.tilinow.dayudengyu(this.tilixiaohao)) {
				//if enough
				if (this.checkalltotal(n)) {
					//jinjie ok manzu up
					this.tilinow.jian(this.tilixiaohao);
					if (wudaoshi == 0) {
						//0 hao wudaoshi;
						let liliangadd =(this.jiangli)
						let minjieadd = (this.jiangli)
						let zhiliadd = (this.jiangli)
						this.liliang.add(liliangadd);
						this.minjie.add(minjieadd);
						this.zhili.add(zhiliadd);
						sb.success = true
						sb.liliangadd = liliangadd
						sb.minjieadd = minjieadd
						sb.zhiliadd = zhiliadd
						return sb;
					}


				} else {
					sb.success = false
					sb.errdetail = "jinjieka"
					return sb;
				}
			} else {
				sb.success = false
				sb.errdetail = "meitili"
				return sb;
			}


		}
		/**
		 * check all total if manzutiaojian
		 * true  manzutiaojian can up
		 * false  no
		 */
		private checkalltotal(n: number): boolean {
			if ((this.liliang.add(this.minjie).add(this.zhili)).dayudengyu(this.getjinjietotal(n))) {
				return false
			} else {
				return true
			}
		}


		/**
		 * get jinjie total
		 */
		private getjinjietotal(n: number): diansha.shuzi {
			let shuzi: diansha.shuzi = this.shuzifactory.get(0, "n");
			shuzi.add(this.shuzifactory.get(100, "n")).add(this.shuzifactory.get(100, "n").cheng(this.shuzifactory.get(n, "n")))
			return shuzi;
		}







		public static getinstance(): shuxing_manage {
			if (this.D == null) {
				this.D = new shuxing_manage();
			}
			return this.D;
		}
	}
}